package org.NooLab.itexx.app.gui.handlers;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

 
import org.NooLab.itexx.ITexxWindowCommons;
import org.NooLab.itexx.app.gui.definition.ITexxWindowGuiIntf;
import org.NooLab.utilities.logging.PrintLog;

import com.sun.jna.examples.WindowUtils;




public class WindowAppearance2 implements WindowAppearanceIntf{

	
	private ITexxWindowGuiIntf windowGui;
	private JFrame frame;
	
	
	private boolean floaters;
	private boolean windowIsTransparent;
	
	private ComponentPkg componentPkg;
	ArrayList<ComponentPkg> componentPkgList = new ArrayList<ComponentPkg>();
	private PrintLog out;
	
	long lastToggleTime = System.currentTimeMillis();
	private ITexxWindowCommons appCommons;
	
	// ========================================================================
	public WindowAppearance2(ITexxWindowGuiIntf gui){
		
		windowGui = gui;
		appCommons = windowGui.getTexxWindowApp().getAppCommons();
		frame = windowGui.getTexxWindowApp().getMainFrame();

		out = appCommons.out ;
	}
	// ========================================================================
	
	@Override
	public ITexxWindowGuiIntf getWindowGui(){
		return windowGui;
	}
	
	@Override
	public void exitApplication() {
		// 
		// message that we will leave...
		appCommons.getApp().close();
	}

	@Override
	public void toggleSimpleView(boolean isSimple) {
		// 
		
	}

	
	public void _toggleSimpleView(int target) {
		
		ComponentPkg cp;
		cp=null;
		int n=componentPkgList.size()-1;
		out = windowGui.getOut();
		
		
		if (System.currentTimeMillis() - lastToggleTime<60){
			return;
		}
		lastToggleTime = System.currentTimeMillis();	
		
		for (int i=0;i<componentPkgList.size();i++){
			
			cp = componentPkgList.get(i);
			
			if ((target == 2) && (cp.mode==2)){
				if (windowIsTransparent) {
					WindowUtils.setWindowAlpha(frame, 1f);
					cp.component.setVisible(true);
					cp.component.setOpaque(true);
					cp.component.setBackground( windowGui.getMappedColor( cp.component,1f ) ) ;
				}else{
					WindowUtils.setWindowAlpha(frame, 0.75f);	
					cp.component.setVisible(true);
					cp.component.setOpaque(false);
					cp.component.setBackground( windowGui.getMappedColor( cp.component,0.2f ) ) ;
				}
			}
			if ((target==1) && (cp.mode==1)){
				if (floaters){
					cp.component.setBackground( windowGui.getMappedColor( cp.component,1f )) ;
				}else{
					Color c =  windowGui.getMappedColor( cp.component,0f );
					cp.component.setBackground( c) ;					
					cp.component.setBackground( windowGui.getMappedColor( cp.component,0f ) ) ;
				}
			}
		}// i->
		
		if ((target == 2) ) {
			if (windowIsTransparent) {
				windowIsTransparent = false;
			} else {
				windowIsTransparent = true;
			}
		}
		if ((target == 1)) {
			if (floaters) {
				floaters = false;
			} else {
				floaters = true;
			}
		}

		n=0;
	}
	/**
	 * here we arrive via alt-w   
	 */
	@Override
	public void toggleSimpleView(int target) {
		// 
		
		// System.out.println("toggling window view ... simple <-> complete (parameter <target> = "+target+")") ;

		if (target == 101) {
			
			
			int direction = -1;
			if (windowGui.isSimpleWindow()) {
				direction = 1;
				
				
			} 
			windowGui.relocateFoundItemsPanel(direction);
			
			{
				
				PrintLog.Delay(100);
				if (direction < 0) {
					prepareSimpleView(2);
					prepareSimpleView(1);
					
				} else {
					prepareFullView(3);
					return;
				}
			}
		}

	}

	private void prepareFullView(int mode) {
		// 
		
	}
	/**
	 *  here we arrive via ctrl-w
	 */
	public void prepareSimpleView(int target) {
		
		Color c; 
		
		try{

			JPopupMenu popupMenu = windowGui.getResultPanelPopupMenu().getPanelPopupMenu();
			 
		if ((target == 3) ){// && (windowGui.isSimpleWindow()
			// make everything visible
			
			// windowGui.getOut().print(2, "t=3");
			
			WindowUtils.setWindowAlpha(frame, 1f);
			WindowUtils.setWindowTransparent(frame, false);
			
			windowIsTransparent = false;

			assignMappedColor( windowGui.getTopPanel(), true, 0.95f );
			/*
			windowGui.getTopPanel().setOpaque(true);
			c = windowGui.getMappedColor( windowGui.getTopPanel() );
			windowGui.getTopPanel().setBackground( new Color( c.getRed(),c.getGreen(),c.getBlue(),0.95f));
			 */
			assignMappedColor( windowGui.getContentPanel(), true, 1f );
			/*
			windowGui.getContentPanel().setOpaque(true);
			c = windowGui.getMappedColor( windowGui.getContentPanel() );
			windowGui.getContentPanel().setBackground( new Color( c.getRed(),c.getGreen(),c.getBlue(),1f));
			*/
			
			assignMappedColor( windowGui.getPanelFound(), true, 1f );
			// windowGui.getPanelFound().setOpaque(true);
			// windowGui.getPanelFound().setVisible(true);
			
			windowGui.getPanelFound().setOpaque(true);
			windowGui.getTabbedPaneMain().setBackground( new Color( 0.5f,0.5f,0.5f,1f));

			windowGui.getTabbedPaneMain().setBackground( new Color( 0.8f,0.8f,0.8f,1f));
			
			windowGui.getPanelFound().setBackground( windowGui.getMappedColor( windowGui.getPanelFound(),1f )) ; 
														// new Color( 0.8f,0.2f,0.2f,0.5f)); // <<<<<<<<<<< set to 0.0f !!!
			
			
			windowGui.getPanelFound().setBackground( windowGui.getMappedColor( windowGui.getPanelFoundTab(),1f )) ;
			
			windowGui.getJPanelMainStatusBar().setOpaque(false);
			windowGui.getJPanelMainStatusBar().setBackground( new Color( (208.0f/255.0f),(208f/255f),(218f/255f),0f) );

			windowGui.getjPanelFoundScrollerHost().setBackground( new Color( 0.67f,0.64f,0.61f,1f) );

			assignMappedColor(windowGui.getjPanelScrollWrapFoundStack(), false, 0.9f) ;
			// windowGui.getjPanelScrollWrapFoundStack().setBackground( new Color( 0.3f,0.3f,0.3f,0.9f) );
			assignMappedColor(windowGui.getScrollPaneReturnedResults(), false, 0.9f) ;
			// windowGui.getScrollPaneReturnedResults().setBackground( new Color(  0.3f,0.3f,0.3f,0.9f) );

			frame.repaint();
			frame.setSize(frame.getWidth()+2, frame.getHeight());
			frame.repaint();
			frame.setSize(frame.getWidth()-2, frame.getHeight());
			

		}else{
			
			WindowUtils.setWindowTransparent(frame, true);
			 
		}
		if (target == 2) {
			if (windowIsTransparent) {
				WindowUtils.setWindowAlpha(frame, 1f);
				// WindowUtils.setWindowTransparent(frame, false);
				windowIsTransparent = false;
				windowGui.getTopPanel().setOpaque(true);
				// windowGui.getTopPanel().setVisible(true);
				// windowGui.getOut().print(2, "t=2a");	
				// windowGui.getContentPanel().setVisible(true);
				// windowGui.getContentPanel().setOpaque(true);
				
				// windowGui.getPanelFound().setVisible(true);
				// windowGui.getPanelFound().setOpaque(true);
			} else {
				// windowGui.getOut().print(2, "t=2b");
				// we have to rearrange 
				//WindowUtils.setWindowTransparent(frame, true);
				WindowUtils.setWindowAlpha(frame, 0.9f);
				windowIsTransparent = true;
				
				windowGui.getTopPanel().setOpaque(false);
				// windowGui.getTopPanel().setBackground( new Color( 0.8f,0.2f,0.2f,0.05f));
				assignMappedColor(windowGui.getTopPanel(), false, 0.03f) ;
				
				windowGui.getContentPanel().setOpaque(false);
				// windowGui.getContentPanel().setBackground( new Color( 0.8f,0.2f,0.2f,0f));
				assignMappedColor(windowGui.getContentPanel(), false, 0f) ;
				
				windowGui.getPanelFound().setOpaque(false);
				// windowGui.getPanelFound().setVisible(true);
				
				windowGui.getPanelFound().setOpaque(false);
				windowGui.getTabbedPaneMain().setBackground( new Color( 0.5f,0.5f,0.8f,0f));
				
				windowGui.getjPanelScrollWrapFoundStack().setOpaque(true);
				windowGui.getScrollPaneReturnedResults().setOpaque(true);
				
				windowGui.getJPanelMainStatusBar().setOpaque(true);
				windowGui.getJPanelMainStatusBar().setBackground( new Color( 0.6f,0.6f,0.6f,0.01f) );
				
				assignMappedColor(windowGui.getjPanelScrollWrapFoundStack(), false, 0.01f) ;
				windowGui.getjPanelScrollWrapFoundStack().setBackground( new Color( 0.3f,0.3f,0.3f,0.01f) );
				windowGui.getScrollPaneReturnedResults().setBackground( new Color(  0.3f,0.3f,0.3f,0.01f) );
			
			}
		}
		
		
		if (target==1){
			if (floaters){
				// windowGui.getOut().print(2, "t=1a");
				floaters=false;
				// JBackgroundPanel panel;
				windowGui.getTopPanel().setBackground( new Color( 0.5f,0.5f,0.5f, 0.05f)); // <<<<<<<<<<<<<<<<<<
				
				// windowGui.getContentPanel().setBackground( new Color( 0.8f,0.2f,0.2f,1f));
				
				windowGui.getTabbedPaneMain().setBackground( new Color( 0.5f,0.5f,0.5f,0f));
				// windowGui.getPanelFoundTab().setBackground( new Color( 0.8f,0.8f,0.2f,0.7f));
				// windowGui.getPanelFound().setBackground( windowGui.getMappedColor( windowGui.getPanelFound(),1f ));
				windowGui.getjPanelScrollWrapFoundStack().setBackground( new Color( 0.3f,0.3f,0.3f,0.01f) );
				windowGui.getScrollPaneReturnedResults().setBackground( new Color(  0.3f,0.3f,0.3f,0.01f) );
				
				// windowGui.getJPanelMainStatusBar() we also have to relocate it!!
				
				Color bc = popupMenu.getBackground();
				popupMenu.setBackground( new Color( bc.getRed()/255f,bc.getGreen()/255f,bc.getBlue()/255f,0.9f) );
 				
			}else{
				floaters=true;
				// windowGui.getOut().print(2, "t=2b");
				windowGui.getContentPanel().setBackground( new Color( 0.6f,0.6f,0.6f,0f));
				windowGui.getTabbedPaneMain().setBackground( new Color( 0.5f,0.5f,0.6f,0f));
				windowGui.getPanelFoundTab().setBackground( new Color( 0.6f,0.6f,0.6f,0f));
				windowGui.getPanelFound().setBackground( windowGui.getMappedColor( windowGui.getPanelFound(),0f )) ; 
															// new Color( 0.8f,0.2f,0.2f,0.5f)); // <<<<<<<<<<< set to 0.0f !!!
				
				windowGui.getTabbedPaneMain().setBackground( new Color( 0.67f,0.64f,0.61f,0f) );
				windowGui.getjPanelFoundScrollerHost().setBackground( new Color( 0.67f,0.64f,0.61f,0f) );

				windowGui.getjPanelScrollWrapFoundStack().setBackground( new Color( 0.5f,0.5f,0.5f,0.01f) );
				windowGui.getScrollPaneReturnedResults().setBackground( new Color(  0.5f,0.5f,0.5f,0.01f) );
				
			}
		}
		
		}catch(Exception e){
			System.out.println(""+e.getMessage()) ;
		}
	}

	
	private void assignMappedColor(JComponent component, boolean opaque, float opaqueness) {
		Color c;
		float ff=255.0f;
		try{
			windowGui.getTopPanel().setOpaque(true);
			c = windowGui.getMappedColor( component );
			component.setBackground( new Color( c.getRed()/ff,c.getGreen()/ff,c.getBlue()/ff, opaqueness));	
		}catch(Exception e){
			windowGui.getOut().printErr(2, "Color not found for component : "+component.toString());
		}
		
	}
	
	@Override
	public void addComponent(JComponent component, int mode, float... values) {
		//
		componentPkg = new ComponentPkg();
		
		componentPkg.component = component;
		componentPkg.mode = mode;
		if (values!=null){
			componentPkg.values = new float[values.length];
			for (int i=0;i<values.length;i++){
				componentPkg.values[i] = values[i] ;	
			}
		}
		
		componentPkgList.add( componentPkg ) ;
		
	}

	@Override
	public void perform(int value) {
		// 
		
	}

	@Override
	public void clearResultList(int parameter) {
		//
		
		
		System.out.println( "clear Result List ...");
		
		out.delay(10);
		
		// windowGui
	}


}

class ComponentPkg{

	public float[] values;
	public int mode;
	public JComponent component;
	
	
}









